﻿using SuperSurvey.BUS;
using SuperSurvey.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SuperSurvey
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        private LoginBUS login;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                HttpCookie cookie = Request.Cookies["UserLogin"];
                if(cookie!=null){
                    txtpassword.Text = cookie["password"].ToString();
                    var pass = cookie["Username"].ToString();
                    txtUseName.Text = pass;
                }
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            login = new LoginBUS();
            string usename = txtUseName.Text.Trim();
            string password = txtpassword.Text.Trim();
            UserLogin userlogin = login.logins(usename);
            if (userlogin != null)
            {
                if (userlogin.Password.Equals(password) == true)
                {

                    Session["Logout"] = "Logout";
                    Session["UserLogin"] = userlogin.UserName;
                    Response.Redirect("index.aspx");
                    if (cbKeepAcc.Checked == true)
                    {
                        HttpCookie cookie = new HttpCookie("UserLogin");
                        cookie["Username"] = userlogin.UserName;
                        cookie["password"] = userlogin.Password;
                        cookie.Expires = DateTime.Now.AddYears(1);
                        Response.Cookies.Add(cookie);
                    }
                    else
                    {
                        Response.Cookies["UserLogin"].Expires = DateTime.Now; DateTime.Now.AddYears(-1);
                    }

                }
                else
                {
                    txtpassword.Focus();
                    lblNotice.Text = "The requested user '" + usename + "' could not be found.";
                    lblNotice.ForeColor = System.Drawing.Color.Red;
                }
            }
            else
            {
                lblNotice.Text = "The requested user '" + usename + "' could not be found.";
                lblNotice.ForeColor = System.Drawing.Color.Red;
                txtpassword.Focus();
            }


        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtpassword.Text = "";
            txtUseName.Text = "";
            cbKeepAcc.Checked = false;
        }
    }
}